package com.dmapp.farmland.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.emapp.lccode.R;
import com.emapp.lccode.base.BaseConfig;
import com.emapp.lccode.base.EventBusConfig;
import com.emapp.lccode.base.EventBusModel;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 * Author:YanshanZhang
 * E-mail:690810892@qq.com
 * Date: 2021/6/16 11:09
 * Desc:
 */
public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, BaseConfig.APPID_WEIXIN);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                Log.e("WXPayEntryActivity", "onReq: "+ req.toString());
               // goToGetMsg();
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                Log.e("WXPayEntryActivity", "onReq: "+ req.toString());
                //goToShowMsg((ShowMessageFromWX.Req) req);
                break;
            default:
                break;
        }
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        // 0成功，展示成功页面
        // -1错误，可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
        // -2用户取消，无需处理。发生场景：用户不支付了，点击取消，返回APP。

		Log.d("WXPayEntryActivity", "onPayFinish, errCode = " + resp.errCode);
//		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle("提示");
//			builder.setMessage("errCode=" + resp.errCode);
//			builder.show();
//		}
//        Intent it = new Intent();
//        it.setAction("com.kmapp.bczj.wxpay");
//        it.putExtra("code", resp.errCode);
//        sendBroadcast(it);
        EventBus.getDefault().post(new EventBusModel(EventBusConfig.PAY_WECHAT,resp.errCode));
        finish();
    }
}
